package com.fly.demo.web;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fly.core.utils.ShellExecutor;
import com.fly.demo.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.ImmutableMap;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * Swagger 注解演示
 * 
 * @author 00fly
 * @version [版本号, 2021年9月11日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@Api(tags = "RestData")
@RestController
@RequestMapping("/data")
@ConditionalOnWebApplication
public class RestDataController
{
    @ApiOperationSupport(order = 1)
    @ApiOperation("post")
    @PostMapping("/post")
    public JsonResult<?> post()
        throws IOException
    {
        File run = new File("/run.jar");
        if (!run.exists())
        {
            Resource jar = new ClassPathResource("easy-json-simple-0.0.1.jar");
            FileUtils.copyURLToFile(jar.getURL(), run);
        }
        String result = ShellExecutor.execute("java -Dfile.encoding=utf-8 -jar /run.jar");
        log.info(result);
        return JsonResult.success(result);
    }
    
    @ApiOperationSupport(order = 10)
    @ApiOperation("get001")
    @GetMapping("/get001")
    public JsonResult<?> get001()
    {
        // 借用MapSqlParameterSource实现chain map
        MapSqlParameterSource map = new MapSqlParameterSource().addValue("k1", "v1").addValue("k2", "v2");
        return JsonResult.success(map.getValues());
    }
    
    @ApiOperationSupport(order = 20)
    @ApiOperation("get002")
    @GetMapping("/get002")
    public JsonResult<?> get002()
    {
        // 借用MapSqlParameterSource实现chain map
        MapSqlParameterSource map = new MapSqlParameterSource("stuName", "jackson").addValue("k1", "v1").addValue("k2", "v2");
        return JsonResult.success(map.getValues());
    }
    
    @ApiOperationSupport(order = 30)
    @ApiOperation("get003")
    @GetMapping("/get003")
    public JsonResult<?> get003()
    {
        // guava
        Map<String, String> immutableMap = new ImmutableMap.Builder<String, String>().put("q1", "java").put("q2", "python").build();
        return JsonResult.success(immutableMap);
    }
}
